package edu.seu.mart.controller;


import edu.seu.mart.service.IChatService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;

/**
 * Created by zj on 2015/3/11.
 */
@Controller
@RequestMapping("/")
public class ChatController {

    @Resource
    private IChatService chatService;


    /**
     * 商品管理模块
     */
    @RequestMapping(value = "/chat", method = RequestMethod.GET)
    public String chatUI() {
        return "chat";
    }

    @RequestMapping(value = "/chat", method = RequestMethod.POST)
    public void chat(@RequestParam String str, HttpServletResponse response) {

        System.out.println("str:" + str);
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html");
        File indexDir = new File(chatService.getIndexDir());
        if (!indexDir.exists()) {
            chatService.creatIndexDir();
        }
        try {
            if (str.startsWith("<img src")) {
                response.getWriter().print(str);
            } else {
                response.getWriter().print(chatService.chat(str));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
